package tr.edu.ege.seagent.rdf.reader.tutorial;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class MyLogger {
	static final String LOGGER_NAME = "MyLogger";
	static private FileHandler fileTxt;
	static private SimpleFormatter formatterTxt;

	static public void setup() throws IOException {

		// Create Logger
		Logger logger = Logger.getLogger(LOGGER_NAME);
		logger.setLevel(Level.INFO);

		Logger rootLogger = getRootLogger(logger);
		Handler[] handlers = rootLogger.getHandlers();

		for (int i = 0; i < handlers.length; i++) {
			System.out.println("handler : " + handlers[i]);
			rootLogger.removeHandler(handlers[i]);
		}

		fileTxt = new FileHandler("Logging.txt");

		// Create txt Formatter
		SimpleFormatter formatter = new SimpleFormatter();
		fileTxt.setFormatter(formatter);
		
		logger.addHandler(fileTxt);

	}

	/**
	 * Retrieves the root logger from the given logger.
	 * 
	 * @param logger
	 *            logger to find root.
	 * @return
	 */
	private static Logger getRootLogger(Logger logger) {
		Logger child = logger;
		Logger parent = logger.getParent();
		do {
			child = parent;
			parent = child.getParent();
		} while (parent != null);

		return child;
	}
}
